local DataModel = import(".DataModel")
local Setting = class("Setting",DataModel)
local SettingHelp = require("app.utils.SettingHelp")
function Setting:ctor()
	Setting.super.ctor(self)
	self:clear()
end

function Setting:clear()
	local defaultSetting = SettingHelp.getDefaultSetting()
	self.hideDailyTaskEntry = false
	self.hideBagRareEntry = false
	self.lastChallengeTipShowTime = 0
	-- 自动战斗
	self.isAutoBattle = false

	self.autoAudio2Text = 1
	self.autoPlayAudioWorld = -1
	self.autoPlayAudioTeam = 1
	self.autoPlayAudioGuild = -1

	self.showWorldBubble = 1
	self.showTeamBubble = 1
	self.showGuildBubble = 1
	self.showTeamreqBubble = 1

	self.showPlayerLevel = defaultSetting.showPlayerLevel
	self.showPlayerUphead = defaultSetting.showPlayerUphead

	self.showAllDamage = false
	self.autoPlayWeather = defaultSetting.autoPlayWeather
	self.cameraZoom = true
	self.videoQuality = 3

	self.lastTStampOfEveryDayLogin = 0
	self.everyDayLoginIdList = ""
	self.oneTimeInSlot = ""

	-- 两侧头像
	self.showBattleHead = true
	-- 友方血量
	self.showBattlePalHp = defaultSetting.showBattlePalHp
	-- 友方名称
	self.showBattlePalName = defaultSetting.showBattlePalName
	-- 角色描边
	self.showBattleOutline = defaultSetting.showBattleOutline
	-- 特效等级
	self.isEffectAllShow = defaultSetting.isEffectAllShow
	EFFECT_ALL_SHOW = defaultSetting.isEffectAllShow
	-- 敌方血量
	self.showBattleFoeHp = defaultSetting.showBattleFoeHp
	-- 敌方名称
	self.showBattleFoeName = defaultSetting.showBattleFoeName
	-- 目标的目标
	self.showBattleTOT = false
	-- 雾气遮罩
	self.showBattleFrog = false
	-- 聊天窗口
	self.showBattleChat = defaultSetting.showBattleChat
	-- 首领预警
	self.showDBM = true

	-- 生成战斗静态背景图片
	self.genBattleStaticBgEnable = defaultSetting.genBattleStaticBgEnable

	-- 自动普攻
	self.keepBasicAttack = false
	-- 主动打开或关闭战场雾气
	self.manualChanged = false

	self.battleTalkContent = {}
	for i = 1, Constant.BattleTalkCount do
		if L(GD:getSystemStrPar("defaultBattleCry"..i, "")) ~= "" then
			self.battleTalkContent[i] = L(GD:getSystemStrPar("defaultBattleCry"..i, ""))
		end
	end

	--聊天综合频道设置
	self.imAllChannelInfoSet = {}
	for i = 1, 6 do
		self.imAllChannelInfoSet[i] = 1
	end
end

function Setting:write(value)
	value.isAutoBattle = self.isAutoBattle

	value.hideDailyTaskEntry = self.hideDailyTaskEntry
	value.hideBagRareEntry = self.hideBagRareEntry

	value.autoAudio2Text = self.autoAudio2Text
	value.autoPlayAudioWorld = self.autoPlayAudioWorld
	value.autoPlayAudioTeam = self.autoPlayAudioTeam
	value.autoPlayAudioGuild = self.autoPlayAudioGuild

	value.showWorldBubble = self.showWorldBubble
	value.showTeamBubble = self.showTeamBubble
	value.showGuildBubble = self.showGuildBubble
	value.showTeamreqBubble = self.showTeamreqBubble

	value.showPlayerLevel = self.showPlayerLevel
	value.showPlayerUphead = self.showPlayerUphead

	value.showAllDamage = self.showAllDamage
	value.autoPlayWeather = self.autoPlayWeather
	value.cameraZoom = self.cameraZoom
	value.videoQuality = self.videoQuality

	value.lastTStampOfEveryDayLogin = self.lastTStampOfEveryDayLogin
	value.everyDayLoginIdList = self.everyDayLoginIdList
	value.oneTimeInSlot = self.oneTimeInSlot

	value.showBattleHead = self.showBattleHead
	value.showBattlePalHp = self.showBattlePalHp
	value.showBattlePalName = self.showBattlePalName
	value.showBattleOutline = self.showBattleOutline
	app:saveValue(Enums.UserDefaultKey.EffectAllShow, EFFECT_ALL_SHOW)
	value.showBattleFoeHp = self.showBattleFoeHp
	value.showBattleFoeName = self.showBattleFoeName
	value.showBattleTOT = self.showBattleTOT
	value.showBattleFrog = self.showBattleFrog
	value.showBattleChat = self.showBattleChat
	value.showDBM = self.showDBM
	value.genBattleStaticBgEnable = self.genBattleStaticBgEnable

	value.keepBasicAttack = self.keepBasicAttack
	value.manualChanged = self.manualChanged

	value.battleTalkContent = {}
	if #self.battleTalkContent == 0 then
		for i = 1, Constant.BattleTalkCount do
			if L(GD:getSystemStrPar("defaultBattleCry"..i, "")) ~= "" then
				value.battleTalkContent[i] = L(GD:getSystemStrPar("defaultBattleCry"..i, ""))
			end
		end
	else
		for i = 1, #self.battleTalkContent do
			if self.battleTalkContent[i] ~= "" then
				value.battleTalkContent[i] = self.battleTalkContent[i]
			elseif L(GD:getSystemStrPar("defaultBattleCry"..i, "")) ~= "" then
				value.battleTalkContent[i] = L(GD:getSystemStrPar("defaultBattleCry"..i, ""))
			end
		end
	end
	value.imAllChannelInfoSet = self.imAllChannelInfoSet

	value.lastChallengeTipShowTime = self.lastChallengeTipShowTime
end

function Setting:read(value)
	if value.hideDailyTaskEntry ~= nil then
		self.hideDailyTaskEntry = value.hideDailyTaskEntry
	end
	if value.hideBagRareEntry ~= nil then
		self.hideBagRareEntry = value.hideBagRareEntry
	end
	
	if value.isAutoBattle ~= nil then
		self.isAutoBattle = value.isAutoBattle
	end

	if value.autoAudio2Text == true then value.autoAudio2Text = 1 end
	if value.autoPlayAudioWorld == true then value.autoPlayAudioWorld = 1 end
	if value.autoPlayAudioTeam == true then value.autoPlayAudioTeam = 1 end
	if value.autoPlayAudioGuild == true then value.autoPlayAudioGuild = 1 end
	if value.showWorldBubble == true then value.showWorldBubble = 1 end
	if value.showTeamBubble == true then value.showTeamBubble = 1 end
	if value.showGuildBubble == true then value.showGuildBubble = 1 end
	if value.showTeamreqBubble == true then value.showTeamreqBubble = 1 end

	if value.autoAudio2Text ~= nil then
		self.autoAudio2Text = value.autoAudio2Text
	end
	if value.autoPlayAudioWorld ~= nil then
		self.autoPlayAudioWorld = value.autoPlayAudioWorld
	end
	if value.autoPlayAudioTeam ~= nil then
		self.autoPlayAudioTeam = value.autoPlayAudioTeam
	end
	if value.autoPlayAudioGuild ~= nil then
		self.autoPlayAudioGuild = value.autoPlayAudioGuild
	end
	if value.showWorldBubble ~= nil then
		self.showWorldBubble = value.showWorldBubble
	end
	if value.showTeamBubble ~= nil then
		self.showTeamBubble = value.showTeamBubble
	end
	if value.showGuildBubble ~= nil then
		self.showGuildBubble = value.showGuildBubble
	end
	if value.showTeamreqBubble ~= nil then
		self.showTeamreqBubble = value.showTeamreqBubble
	end
	if value.showPlayerLevel ~= nil then
		self.showPlayerLevel = value.showPlayerLevel
	end

	if value.showPlayerUphead ~= nil then
		self.showPlayerUphead = value.showPlayerUphead
	end

	if value.showAllDamage ~= nil then
		self.showAllDamage = value.showAllDamage
	end
	if value.autoPlayWeather ~= nil then
		self.autoPlayWeather = value.autoPlayWeather
	end
	if value.cameraZoom ~= nil then
		self.cameraZoom = value.cameraZoom
	end
	if value.videoQuality ~= nil then
		self.videoQuality = value.videoQuality
	end
	if value.lastTStampOfEveryDayLogin ~= nil then
		self.lastTStampOfEveryDayLogin = value.lastTStampOfEveryDayLogin
	end
	if value.everyDayLoginIdList ~= nil then
		self.everyDayLoginIdList = value.everyDayLoginIdList
	end
	if value.oneTimeInSlot ~= nil then
		self.oneTimeInSlot = value.oneTimeInSlot
	end
	if value.showBattleHead ~= nil then
		self.showBattleHead = value.showBattleHead
	end
	if value.showBattlePalHp ~= nil then
		self.showBattlePalHp = value.showBattlePalHp
	end
	if value.showBattlePalName ~= nil then
		self.showBattlePalName = value.showBattlePalName
	end
	if value.showBattleOutline ~= nil then
		self.showBattleOutline = value.showBattleOutline
	end
	EFFECT_ALL_SHOW = app:isEffectAllShow()
	if value.showBattleFoeHp ~= nil then
		self.showBattleFoeHp = value.showBattleFoeHp
	end
	if value.showBattleFoeName ~= nil then
		self.showBattleFoeName = value.showBattleFoeName
	end
	if value.showBattleTOT ~= nil then
		self.showBattleTOT = value.showBattleTOT
	end
	if value.showBattleFrog ~= nil then
		self.showBattleFrog = value.showBattleFrog
	end
	if value.showBattleChat ~= nil then
		self.showBattleChat = value.showBattleChat
	end
	if value.showDBM ~= nil then
		self.showDBM = value.showDBM
	end
	if value.genBattleStaticBgEnable ~= nil then
		self.genBattleStaticBgEnable = value.genBattleStaticBgEnable
	end
	if value.keepBasicAttack ~= nil then
		self.keepBasicAttack = value.keepBasicAttack
	end
	if value.manualChanged ~= nil then
		self.manualChanged = value.manualChanged
	end
	-- 
	if value.battleTalkContent ~= nil and #value.battleTalkContent ~= 0 then
		self.battleTalkContent = {}
		for i = 1, #value.battleTalkContent do
			if value.battleTalkContent[i] ~= "" then
				self.battleTalkContent[i] = value.battleTalkContent[i]
			elseif L(GD:getSystemStrPar("defaultBattleCry"..i, "")) ~= "" then
				self.battleTalkContent[i] = L(GD:getSystemStrPar("defaultBattleCry"..i, ""))
			end
		end
	end
	
	local imAllChannelInfoSet = rawget(value, "imAllChannelInfoSet")
	if imAllChannelInfoSet ~= nil and #imAllChannelInfoSet ~= 0 then
		self.imAllChannelInfoSet = imAllChannelInfoSet
	end

	self.lastChallengeTipShowTime = value.lastChallengeTipShowTime
	if self.lastChallengeTipShowTime == nil then
		self.lastChallengeTipShowTime = 0
	end
end

function Setting:isShowMost()
	return self.showPlayerLevel == "most"
end

function Setting:isShowNormal()
	return self.showPlayerLevel == "normal"
end

function Setting:isShowLittle()
	return self.showPlayerLevel == "little"
end

function Setting:showMostPlayer()
	self:update("showPlayerLevel","most",true)
end

function Setting:showNormalPlayer()
	self:update("showPlayerLevel","normal",true)
end

function Setting:showLittlePlayer()
	self:update("showPlayerLevel","little",true)
end

function Setting:getShowPlayerNum()
	if self:isShowMost() then
		return 30
	elseif self:isShowNormal() then
		return 15
	elseif self:isShowLittle() then
		return 8
	end
	return 8
end

function Setting:isShowPlayerUpHead()
	return self.showPlayerUphead
end

function Setting:setShowPlayerUpHead(show)
	self:update("showPlayerUphead",show,true)
end

function Setting:isShowAllDamage()
	return self.showAllDamage
end

function Setting:setShowAllDamage(show)
	self:update("showAllDamage",show,true)
end

function Setting:isAutoPlayWeather()
	return self.autoPlayWeather
end

function Setting:setAutoPlayWeather(auto)
	self:update("autoPlayWeather",auto,true)
end

function Setting:setCameraZoom(cameraZoom)
	self:update("cameraZoom", cameraZoom ,true)
end

function Setting:isCameraZoom()
	return self.cameraZoom
end

function Setting:onAutoPlayWeatherChanged(cb)
	self.mAutoPlayWeatherChangedHandle = self:onChanged(function(name,old,new)
		if name == "autoPlayWeather" then
			if cb then
				cb(new)
			end
		end
	end)
	return self.mAutoPlayWeatherChangedHandle
end

function Setting:unChangedAutoPlayWeather()
	if self.mAutoPlayWeatherChangedHandle then
		self:unChanged(self.mAutoPlayWeatherChangedHandle)
		self.mAutoPlayWeatherChangedHandle = nil
	end
end

function Setting:onShowPlayerUpheadChanged(cb)
	self.mShowPlayerUpheadHandle = self:onChanged(function(name,old,new)
		if name == "showPlayerUphead" then
			--print("=========>showPlayerUphead")
			if cb then
				cb(new)
			end
		end
	end)
	return self.mShowPlayerUpheadHandle
end

function Setting:unChangedShowPlayerUphead()
	if self.mShowPlayerUpheadHandle then
		self:unChanged(self.mShowPlayerUpheadHandle)
		self.mShowPlayerUpheadHandle = nil
	end
end


--
function Setting:isAutoAudio2Text()
	return self.autoAudio2Text == 1
end
function Setting:setAutoAudio2Text(show)
	self:update("autoAudio2Text",show and 1 or -1)
end
function Setting:isAutoPlayAudioWorld()
	return self.autoPlayAudioWorld == 1
end
function Setting:setAutoPlayAudioWorld(show)
	self:update("autoPlayAudioWorld", show and 1 or -1)
end
function Setting:isAutoPlayAudioTeam()
	return self.autoPlayAudioTeam == 1
end
function Setting:setAutoPlayAudioTeam(show)
	self:update("autoPlayAudioTeam", show and 1 or -1)
end
function Setting:isAutoPlayAudioGuild()
	print( " ------------->>>>> 旧接口:isAutoPlayAudioGuild" )
	return self.autoPlayAudioGuild == 1
end
function Setting:setAutoPlayAudioGuild(show)
	print( " ------------->>>>> 旧接口:setAutoPlayAudioGuild" )
	self:update("autoPlayAudioGuild", show and 1 or -1)
end

function Setting:isShowWorldBubble()
	return self.showWorldBubble == 1
end
function Setting:setShowWorldBubble(show)
	self:update("showWorldBubble", show and 1 or -1)
end
function Setting:isShowTeamBubble()
	return self.showTeamBubble == 1
end
function Setting:setShowTeamBubble(show)
	self:update("showTeamBubble", show and 1 or -1)
end
function Setting:isShowGuildBubble()
	print( " ------------->>>>> 旧接口:isShowGuildBubble" )
	return self.showGuildBubble == 1
end
function Setting:setShowGuildBubble(show)
	print( " ------------->>>>> 旧接口:setShowGuildBubble" )
	self:update("showGuildBubble", show and 1 or -1)
end
function Setting:isShowTeamreqBubble()
	return self.showTeamreqBubble == 1
end
function Setting:setShowTeamreqBubble(show)
	self:update("showTeamreqBubble", show and 1 or -1)
end

function Setting:setVideoQuality(quality)
	self.videoQuality = quality
end

function Setting:getVideoQuality()
	return self.videoQuality
end

function Setting:isHighVideoQuality()
	return self.videoQuality == 1
end

function Setting:isLowVideoQuality()
	return self.videoQuality == 2
end

function Setting:isCustomVideoQuality()
	return self.videoQuality == 3
end

-- 上一次修改 每天第一次登录弹出公告列表 的时间戳
function Setting:getLastModifyTStampOfEveryDayLogin()
	return self.lastTStampOfEveryDayLogin
end
function Setting:setLastModifyTStampOfEveryDayLogin( newTimeStamp )
	self.lastTStampOfEveryDayLogin = newTimeStamp
end

-- 返回 每天第一次登录弹出过的公告列表
function Setting:getEveryDayLoginIdList()
	return self.everyDayLoginIdList
end
function Setting:setEveryDayLoginIdList( newEveryDayLoginIdList )
	self.everyDayLoginIdList = newEveryDayLoginIdList
end

-- 有效时间之内弹出过的 公告列表ID
function Setting:getOneTimeInSlot()
	return self.oneTimeInSlot
end
function Setting:setOneTimeInSlot( newOneTimeInSlot )
	self.oneTimeInSlot = newOneTimeInSlot
end

function Setting:isShowBattleHead()
	return self.showBattleHead
end

function Setting:setShowBattleHead(show)
	self:update("showBattleHead",show,true)
end

function Setting:isShowBattleTOT()
	return self.showBattleTOT
end

function Setting:setShowBattleTOT(show)
	self:update("showBattleTOT",show,true)
end

function Setting:isShowBattleChat()
	return self.showBattleChat
end

function Setting:setShowBattleChat(show)
	self:update("showBattleChat",show,true)
end

function Setting:isShowBattlePalHp()
	return self.showBattlePalHp
end

function Setting:setShowBattlePalHp(show)
	self:update("showBattlePalHp",show,true)
end

function Setting:isShowBattlePalName()
	return self.showBattlePalName
end

function Setting:setShowBattlePalName(show)
	self:update("showBattlePalName",show,true)
end

function Setting:isShowBattleFoeHp()
	return self.showBattleFoeHp
end

function Setting:setShowBattleFoeHp(show)
	self:update("showBattleFoeHp",show,true)
end

function Setting:isShowBattleFoeName()
	return self.showBattleFoeName
end

function Setting:setShowBattleFoeName(show)
	self:update("showBattleFoeName",show,true)
end

function Setting:isShowBattleFrog()
	return self.showBattleFrog
end

function Setting:setShowBattleFrog(show)
	self:update("showBattleFrog",show,true)
	self:setManualChanged(true)
end

function Setting:isManualChanged()
	return self.manualChanged
end

function Setting:setManualChanged(show)
	self:update("manualChanged",show,true)
end

function Setting:isShowBattleOutline()
	return self.showBattleOutline
end

function Setting:setShowBattleOutline(show)
	self:update("showBattleOutline",show,true)
end

function Setting:isKeepBasicAttack()
	return self.keepBasicAttack
end

function Setting:setKeepBasicAttack(b)
	self:update("keepBasicAttack",b,true)
end

function Setting:getIsAutoBattle()
	return self.isAutoBattle
end

function Setting:setAutoBattle(b)
	self:update("isAutoBattle",b,true)
end

function Setting:getBattleTalkContent(idx)
	return self.battleTalkContent[idx] or L(GD:getSystemStrPar("defaultBattleCry"..idx, "")) or ""
end

function Setting:setBattleTalkContent(idx, content)
	self.battleTalkContent = self.battleTalkContent or {}
	self.battleTalkContent[idx] = content or ""
end

function Setting:getIsHideDailyTaskEntry()
	return self.hideDailyTaskEntry
end

function Setting:setHideDailyTaskEntry(b)
	self:update("hideDailyTaskEntry", b, true)
end

function Setting:isShowDBM()
	return self.showDBM
end

function Setting:setShowDBM(b)
	self:update("showDBM", b, true)
end


function Setting:isGenBattleStaticBgEnable()
	return self.genBattleStaticBgEnable
end

function Setting:setGenBattleStaticBgEnable(b)
	self:update("genBattleStaticBgEnable", b, true)
end


function Setting:getIsHideBagRareEntry()
	return self.hideBagRareEntry
end
function Setting:setHideBagRareEntry(b)
	self:update("hideBagRareEntry", b, true)
end

function Setting:setImChannelInfoSet(idx, b)
	self.imAllChannelInfoSet[idx] = (b and 1 or -1)
end
function Setting:getImChannelInfoSet(idx)
	return self.imAllChannelInfoSet[idx] == 1 
end

function Setting:getLastChallengeTipShowTime()
	return self.lastChallengeTipShowTime
end

function Setting:setLastChallengeTipShowTime(time)
	self:update("lastChallengeTipShowTime", time, true)
	UD.dirty = true -- 强制保存一次setting
end

return Setting
